<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('基金简称')" prop="fundName">
            <el-input v-model="form.fundName" :placeholder="$t('基金简称')" maxlength="200" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金规模') + $t('金额单位')" prop="fundScale">
            <Money v-model="form.fundScale" unit="w" @change="calcPercent" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('成立时间')" prop="buildTime">
            <el-date-picker
              v-model="form.buildTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('成立时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资人名称')" prop="lpName">
            <el-input v-model="form.lpName" :placeholder="$t('出资人名称')" maxlength="200" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资金额') + $t('金额单位')" prop="lpMoney">
            <Money v-model="form.lpMoney" :placeholder="$t('出资金额')" unit="w" @change="calcPercent" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资占比(%)')" prop="lpPercent">
            <!-- <Money v-model="form.lpPercent" :placeholder="$t('出资占比')" /> -->
            <Ratio
              v-model="form.lpPercent"
              :precision="2"
              :controls="false"
              :max="100"
              :placeholder="$t('出资金额 / 基金规模 * 100')"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('资金性质')" prop="fundQuality">
            <el-input v-model="form.fundQuality" :placeholder="$t('资金性质')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('介绍')" prop="lpRecommend">
            <el-input v-model="form.lpRecommend" type="textarea" :placeholder="$t('介绍')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:manage" />
    </div>
  </div>
</template>

<script>
    import { saveFundHistoryManage, getFundHistoryManage } from '@/api/fund/gp/history/fundHistoryManage'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  fundName: [{ required: true, message: '必填信息', trigger: 'blur' }],
                  fundScale: [{ required: true, message: '必填信息', trigger: 'blur' }],
                  buildTime: [{ required: true, message: '必填信息', trigger: 'blur' }],
                  lpName: [{ required: true, message: '必填信息', trigger: 'blur' }],
                  lpMoney: [{ required: true, message: '必填信息', trigger: 'blur' }]
                }
            }
        },
        created() {},
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getFundHistoryManage(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    fundName: undefined,
                    fundScale: undefined,
                    buildTime: undefined,
                    lpName: undefined,
                    lpMoney: undefined,
                    gpId: this.$route.query.gpId,
                    lpPercent: undefined,
                    fundQuality: undefined,
                    lpRecommend: undefined,
                    createTime: undefined,
                    createBy: undefined,
                    updateTime: undefined,
                    updateBy: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        this.form.gpId = this.$route.query.gpId
                        saveFundHistoryManage(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           },
           calcPercent() {
              if (this.form.fundScale && this.form.lpMoney) {
                this.form.lpPercent = this.form.lpMoney / this.form.fundScale * 100
              }
           }
        }
    }
</script>
